package com.woniu.carrent.exception;


import com.woniu.carrent.util.ResultMessage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.login.LoginException;

@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {
    @ExceptionHandler(Exception.class) //拦截所有exception并处理
    public ResultMessage<String> handleException(Exception e, HttpServletRequest request) {
        // 输出异常详情和请求信息到日志
        System.err.println("请求地址: " + request.getRequestURI() + ", 异常信息: " + e.getMessage());
        return ResultMessage.fail(e.getMessage());
    }
    @ExceptionHandler(LoginException.class)//登陆异常拦截
    public ResultMessage<String> handleLoginException(LoginException e, HttpServletRequest request) {
        // 输出异常详情和请求信息到日志
        System.err.println("登录异常 - 请求地址: " + request.getRequestURI() + ", 错误信息: " + e.getMessage());
        return ResultMessage.fail(e.getMessage());
    }
}
